package com.zhuoshi.controller;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhuoshi.bean.WdHtProject;
import com.zhuoshi.bean.WdHtRoom;
import com.zhuoshi.bean.WdOpeningReport;
import com.zhuoshi.bean.WdPayType;
import com.zhuoshi.service.OpeningQuotationService;

@Controller
public class OpeningQuotationController {
	@Autowired
	private OpeningQuotationService service;
	
	@RequestMapping("/tobasic.do")
	public String toBasicInformation(Model model){
		List<WdHtProject> list = service.selectProject();
		model.addAttribute("list", list);
		return "basicInformation";
	}
	
	@RequestMapping("/toselectroom.do")
	public String toSelectRoom(WdOpeningReport open,Model model,HttpSession session){
		UUID uuid = UUID.randomUUID();
		String uid = uuid.toString();
		System.out.println(uid);
		open.setOpeningReportId(uid);
		service.addOpening(open);
		WdHtProject project = service.selectProjectById(open.getProjectId());
		List<WdHtRoom> list = service.selectRoomInOpen(uid);
		model.addAttribute("list", list);
		model.addAttribute("project", project);
		session.setAttribute("openid", uid);
		//model.addAttribute("openid", uid);
		return "selectRoom";
	}
	
	@RequestMapping("/updateopen.do")
	public String updateOpen(WdOpeningReport open,Model model,HttpSession session){
		System.out.println(open.getPriceType()+"================");
		String openid = (String) session.getAttribute("openid");
		open.setOpeningReportId(openid);
		service.updateOpen(open);
		WdHtProject project = service.selectProjectById(open.getProjectId());
		List<WdHtRoom> list = service.selectRoomInOpen(openid);
		model.addAttribute("list", list);
		model.addAttribute("project", project);
		return "selectRoom";
	}
	
	@RequestMapping("/toimportprice.do")
	public String toImportPrice(HttpSession session,Model model){
		String openid = (String) session.getAttribute("openid");
		WdHtProject project = service.selectProjectById(service.selectOpenById(openid).getProjectId());
		List<WdHtRoom> list = service.selectRoomInOpen(openid);
		model.addAttribute("pricetype", service.selectOpenById(openid).getPriceType());
		model.addAttribute("list", list);
		model.addAttribute("project", project);
		model.addAttribute("openid", openid);
		return "importPrice";
	}
	
	@RequestMapping("/toimportpayment.do")
	public String toImportPayment(HttpSession session,Model model,Double sumzk,String idlist){
		System.out.println(idlist);
		System.out.println(sumzk);
		String openid = (String) session.getAttribute("openid");
		WdOpeningReport open = new WdOpeningReport();
		open.setOpeningReportId(openid);
		open.setDiscount(sumzk);
		service.updateOpen(open);
		List<WdPayType> list = service.selectPayType();
		model.addAttribute("list", list);
		session.setAttribute("zklist", idlist);
		return "importPayment";
	}
	
	@RequestMapping("/topayview.do")
	public String toPayView(String pay_type_id,Model model){
		System.out.println(pay_type_id);
		WdPayType payType = service.selectPayTypeById(pay_type_id);
		model.addAttribute("pay", payType);
		return "payView";
	}
	
	@RequestMapping("/toimportdiscount.do")
	public String toImportDiscount(){
		return "importDiscount";
	}
	
	@RequestMapping("/tosubmitopen.do")
	public String toSubmitOpen(HttpSession session,Model model,String payid){
		System.out.println(payid);
		String openid = (String) session.getAttribute("openid");
		WdOpeningReport open = new WdOpeningReport();
		open.setOpeningReportId(openid);
		open.setPayTypeId(payid);
		service.updateOpen(open);
		WdOpeningReport openingReport = service.selectOpenById(openid);
		WdHtProject project = service.selectProjectById(openingReport.getProjectId());
		WdPayType payType = service.selectPayTypeById(openingReport.getPayTypeId());
		model.addAttribute("pay", payType);
		model.addAttribute("project", project);
		model.addAttribute("open", openingReport);
		session.setAttribute("payid", payid);
		return "submitOpen";
	} 
	
	@RequestMapping("/submitOpen.do")
	public String submitOpen(HttpSession session){
		String openid = (String) session.getAttribute("openid");
		WdOpeningReport open = new WdOpeningReport();
		open.setOpeningReportId(openid);
		open.setState("审核中");
		service.updateOpen(open);
		session.invalidate();
		return "redirect:getAllShenhe.action";
	} 
	
	@RequestMapping("/toopeningaddroom.do")
	public String toOpeningAddRoom(HttpSession session,Model model){
		String openid = (String) session.getAttribute("openid");
		List<WdHtRoom> roomInOpen = service.selectRoomInOpen(openid);
		List<WdHtRoom> roomNotInOpen = service.selectRoomNotInOpen();
		model.addAttribute("roomInOpen", roomInOpen);
		model.addAttribute("roomNotInOpen", roomNotInOpen);
		model.addAttribute("openid", openid);
		return "openingAddRoom";
	}
	
	@RequestMapping("/addRoomInOpen.do")
	public String addRoomInOpen(HttpSession session,String idlist){
		String openid = (String) session.getAttribute("openid");
		System.out.println("idlist"+idlist);
		List<String> id = Arrays.asList(idlist.split(","));
		for(String roomid:id){
			System.out.println("roomid"+roomid);
			WdHtRoom room = new WdHtRoom();
			room.setRoomId(roomid);
			room.setOpeningReportId(openid);
			service.addRoomInOpen(room);
		}
		return "redirect:toopeningaddroom.do?openid="+openid;
	}
	
	@RequestMapping("/addAllRoomInOpen.do")
	public String addAllRoomInOpen(HttpSession session){
		String openid = (String) session.getAttribute("openid");
		service.addAllRoomInOpen(openid);
		return "redirect:toopeningaddroom.do?openid="+openid;
	}
	
	@RequestMapping("/removeRoomInOpen.do")
	public String removeRoomInOpen(HttpSession session,String idlist){
		String openid = (String) session.getAttribute("openid");
		System.out.println("idlist"+idlist);
		List<String> id = Arrays.asList(idlist.split(","));
		for(String roomid:id){
			System.out.println("roomid"+roomid);
			WdHtRoom room = new WdHtRoom();
			room.setRoomId(roomid);
			room.setOpeningReportId(openid);
			service.removeRoomInOpen(room);
		}
		return "redirect:toopeningaddroom.do?openid="+openid;
	}
	
	@RequestMapping("/removeAllRoomInOpen.do")
	public String removeAllRoomInOpen(HttpSession session){
		String openid = (String) session.getAttribute("openid");
		service.removeAllRoomInOpen(openid);
		return "redirect:toopeningaddroom.do?openid="+openid;
	}
	
	@RequestMapping("/removeRoomInOpenBackRoom.do")
	public String removeRoomInOpenBackRoom(HttpSession session,String idlist){
		String openid = (String) session.getAttribute("openid");
		System.out.println("idlist"+idlist);
		List<String> id = Arrays.asList(idlist.split(","));
		for(String roomid:id){
			System.out.println("roomid"+roomid);
			WdHtRoom room = new WdHtRoom();
			room.setRoomId(roomid);
			room.setOpeningReportId(openid);
			service.removeRoomInOpen(room);
		}
		return "redirect:backtoselectroom.do?openid="+openid;
	}
	
	@RequestMapping("/removeAllRoomInOpenBackRoom.do")
	public String removeAllRoomInOpenBackRoom(HttpSession session){
		String openid = (String) session.getAttribute("openid");
		service.removeAllRoomInOpen(openid);
		return "redirect:backtoselectroom.do?openid="+openid;
	}
	
	@RequestMapping("/backtobasic.do")
	public String backToBasicInformation(HttpSession session,Model model){
		String openid = (String) session.getAttribute("openid");
		List<WdHtProject> list = service.selectProject();
		WdOpeningReport open = service.selectOpenById(openid);
		model.addAttribute("open", open);
		model.addAttribute("list", list);
		return "basicInformation";
	}
	
	@RequestMapping("/backtoselectroom.do")
	public String backToSelectRoom(HttpSession session,Model model){
		String openid = (String) session.getAttribute("openid");
		WdHtProject project = service.selectProjectById(service.selectOpenById(openid).getProjectId());
		List<WdHtRoom> list = service.selectRoomInOpen(openid);
		model.addAttribute("list", list);
		model.addAttribute("project", project);
		model.addAttribute("openid", openid);
		return "selectRoom";
	}
	
	@RequestMapping("/backtoimportprice.do")
	public String backToImportPrice(HttpSession session,Model model){
		String openid = (String) session.getAttribute("openid");
		WdHtProject project = service.selectProjectById(service.selectOpenById(openid).getProjectId());
		List<WdHtRoom> list = service.selectRoomInOpen(openid);
		model.addAttribute("pricetype", service.selectOpenById(openid).getPriceType());
		model.addAttribute("list", list);
		model.addAttribute("project", project);
		return "importPrice";
	}
	
	@RequestMapping("/backtoimportpayment.do")
	public String backToImportPayment(HttpSession session,Model model){
		List<WdPayType> list = service.selectPayType();
		model.addAttribute("list", list);
		return "importPayment";
	}
	
	@RequestMapping("/backtoimportdiscount.do")
	public String backToImportDiscount(){
		return "importDiscount";
	}
	
	
}
